<?php
namespace app\spadmin\controller\system;
use app\common\controller\Backend;
/**
 * 广告位
 */
class Adverts extends Backend{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Adverts');
    }

    public function index(){
        $this->request->filter(['strip_tags', 'trim']);
        $position_id = $this->request->get('position_id',0);
        if($this->request->isAjax()){
            
            
            $data = $this->request->param();
            $map['position_id'] = $data['position_id'];
            if (isset($data['keywords']) && $data['keywords']) {
                $map['title'] = ['like', '%' . $data['keywords'] . '%'];
            }
           
            if (isset($data['status']) && $data['status']) {
                $map['status'] = $data['status'];
            } else {
                $map['status'] = 1;
            }           
            $list =   $this->model->where($map)->order('sort desc')->paginate($data['limit'])->toArray();
            
            return $this->result($list);
        }
        if($position_id == 0){
            $this->error('广告位找不到');
        }
        $position = model('Position')->where('id',$position_id)->value('title');
        $this->assign('position',$position);
        return $this->view->fetch();
    }
    public function edit($ids=null){
        $row = $this->model->get($ids);
        if($this->request->isPost()){
            if($row == null){
                $row = $this->model;
            }
            $data = $this->request->post('',[],'strip_tags');
            
            $result = $row->validate()->allowField(true)->save($data,$data['id']?['id'=>$data['id']]:null);
            if($result === false){
                $this->error($row->getError());
            }
            $this->success('操作成功');
        }
        $this->assign('info',$row);
        return $this->view->fetch();
    }
}